﻿package scripts
{
	import com.greensock.TweenLite;
	import flash.display.Bitmap;
	import flash.display.BitmapData;
	import flash.display.Sprite;
	import flash.display.SimpleButton;
	import flash.display.MovieClip;
	import flash.display.Loader;
	import flash.display.Shape;
	import flash.display.GradientType;
	import flash.display.SpreadMethod;

	import flash.geom.Point;
	import flash.geom.Matrix;
	import flash.filters.DropShadowFilter;
	import flash.net.URLRequest;
	import flash.utils.Timer;
	import flash.utils.*;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.events.ProgressEvent;
	import flash.events.TimerEvent;
	import flash.filters.BlurFilter;
	import flash.filters.BitmapFilterQuality;
	import flash.display.Stage;
	import flash.system.Security;

	public class PageFlipClass
	{
		//可设置或可调用接口,页数以单页数计算~---------------------------------------
		public var myXML:XML;
		public var book_root:MovieClip;//装载book的MC
		public var book_initpage:Number = 0;//初始化到第N页
		public var book_totalpage:Number = 0;//总页数
		public var book_TimerNum:Number = 100;//Timer的间隔时间
		public var book_page:Number = 1;//当前页
		public var onLoadinit:Function = null;//加载外部影片或图片时调用
		public var onLoading:Function = null;//正在加载外部影片或图片时调用
		public var onLoadEnd:Function = null;//加载外部影片或图片完毕时调用
		public var onPageEnd:Function = null;//翻页动作结束时调用
		public var downHandler:Function = null;
		public var upHandler:Function = null;
		//PageGoto:Function;//翻页跳转
		//PageDraw:Function;//绘制缩略图
		//InitBook:Function;//初始化
		//END!!--------------------------------------------------------------------

		private var book_width:Number;
		private var book_height:Number;
		private var book_topage:Number;


		private var book_CrossGap:Number;
		private var bookArray_layer1:Array;
		private var bookArray_layer2:Array;
		private var _allMiddleMCs:Array=new Array();// MidMc array is here

		private var book_TimerFlag:String = "stop";
		private var book_TimerArg0:Number = 0;
		private var book_TimerArg1:Number = 0;
		private var u:Number;
		private var book_px:Number = 0;
		private var book_py:Number = 0;
		private var book_toposArray:Array;
		private var book_myposArray:Array;
		private var book_timer:Timer;

		private var Bmp0:BitmapData;
		private var Bmp1:BitmapData;
		private var bgBmp0:BitmapData;
		private var bgBmp1:BitmapData;

		private var pageMC:Sprite=new Sprite();
		private var bgMC:Sprite=new Sprite();

		private var render0:Shape=new Shape();
		private var render1:Shape=new Shape();
		private var shadow0:Shape=new Shape();
		private var shadow1:Shape=new Shape();

		private var Mask0:Shape=new Shape();
		private var Mask1:Shape=new Shape();

		private var p1:Point;
		private var p2:Point;
		private var p3:Point;
		private var p4:Point;

		private var limit_point1:Point;
		private var limit_point2:Point;

		private var preFlipBtnTL:preFlipBtn_L;
		private var preFlipBtnTR:preFlipBtn_R;
		private var preFlipBtnBL:preFlipBtn;
		private var preFlipBtnBR:preFlipBtn;
		/*
		Geordi added
		*/
		private var _middleAnimationMC:GeoTween=new GeoTween();
		private var Dragflag:Number = 0;
		private var _curPageIndex:Number = 1;//important! for counting pages.
		private var _curXMLIndex:Number = 0;//important! for counting xml page rows.
		public var _xmlTotalPage:Number;
		public var printflag:Boolean = false;
		private var globle_scale:Number;
		public var mystage:Stage;
		private var all_pagesArray:Object = new Object();
		private var pb:Progress_Bar;
		public var output:MovieClip;

		//**init Parts------------------------------------------------------------------------
		public function InitBook():void
		{
			Security.allowDomain("*");
			book_width = myXML.attribute("width");
			book_height = myXML.attribute("height");
			
			globle_scale = book_width /1000;

			book_page = book_topage = book_initpage;
			book_CrossGap = Math.sqrt(book_width * book_width + book_height * book_height);
			/**
			 * 创建书页上的四个角，作为四个最终点
			 * */
			p1 = new Point(0,0);
			p2 = new Point(0,book_height);
			p3 = new Point(book_width + book_width,0);
			p4 = new Point(book_width + book_width,book_height);
			
			/**
			 * 临界点， limit_point1是中线顶点，limit_point2是中线底点； 
			 * */
			limit_point1 = new Point(book_width,0);
			limit_point2 = new Point(book_width,book_height);
			/**
			 * 向book_toposArray依次放入整页的
			 * 			右上角点P3，
			 * 			右下角点P4，
			 * 			左上角点p1，
			 * 			左下角点p2；
			 * */
			book_toposArray = [p3,p4,p1,p2];
			/**
			 * 向book_myposArray依次放入整页的
			 * 			左上角点：p1
			 * 			左下角点：p2
			 * 			右上角点：p3
			 * 			右下角点：p4
			 * */
			book_myposArray = [p1,p2,p3,p4];
			/**
			 * pageMC是一个空sprite
			 * bgMC是一个空sprite
			 * */
			book_root.addChild(pageMC);
			book_root.addChild(bgMC);
			/**
			 * 分别给pageMc和bgMC加上阴影
			 * */
			SetFilter(pageMC);
			SetFilter(bgMC);
			/**
			 * 添加两个遮罩层Mask0,和mask1
			 * */
			book_root.addChild(Mask0);
			book_root.addChild(Mask1);

			book_root.addChild(render0);
			book_root.addChild(shadow0);
			book_root.addChild(render1);
			book_root.addChild(shadow1);
			_middleAnimationMC.y = 0;
			_middleAnimationMC.alpha = 0;
			book_root.parent.addChild(_middleAnimationMC);
			
			pb=new Progress_Bar();
			pb.y=book_height*0.5;
			mystage.addChild(pb);
			book_root.parent.visible=false;
			
			
			/**
			 * 加载xml中的第一个swf文件, 加载完成后会自动回调setLoadMC来加载第二个swf文件
			 * */
			SetLoadMC();

			book_timer = new Timer(book_TimerNum);

			InitpreFlipBtn();


			book_timer.addEventListener("timer", bookTimerHandler);
		}
		//**Setting Parts------------------------------------------------------------------------
		private function SetFilter(obj):void
		{
			var filter:DropShadowFilter=new DropShadowFilter();
			filter.blurX = filter.blurY = 10;
			filter.alpha = 0.5;
			filter.distance = 0;
			filter.angle = 0;
			obj.filters = [filter];
		}
		/*
		Loading external swf
		*/
		private function SetLoadMC():void
		{
			//!printflag || trace(0);
			var pageRequest:URLRequest = new URLRequest(myXML.pages.child("page")[_curXMLIndex].attribute("src"));
			var contentLoader:Loader=new Loader();
			//contentLoader.contentLoaderInfo.addEventListener(ProgressEvent.PROGRESS,loadProgress);
			contentLoader.contentLoaderInfo.addEventListener(Event.COMPLETE,LoadEnd);
			contentLoader.load(pageRequest);
		}		
		/*
		* after swf file loaded, take page_Left and page_right out from swf and put them in each page.
		*/
		private function LoadEnd(evtObj:Event):void
		{
			!printflag || trace("LoadEnd");
			var obj:MovieClip = MovieClip(evtObj.target.loader.content);
			//如果swf中有page_Left对象
			if (obj.hasOwnProperty("page_Left") && (obj["page_Left"] is MovieClip))
			{
				obj["page_Left"].scaleX = obj["page_Left"].scaleY = globle_scale;
				all_pagesArray["pload_" + _curPageIndex] = (obj["page_Left"] as MovieClip);
				_curPageIndex++;
			}
			if (obj.hasOwnProperty("page_Right") && (obj["page_Right"] is MovieClip))
			{
				obj["page_Right"].scaleX = obj["page_Right"].scaleY = globle_scale;
				obj["page_Right"].x = 0;
				all_pagesArray["pload_" + _curPageIndex] = (obj["page_Right"] as MovieClip);
				_curPageIndex++;
			}
			if (obj.hasOwnProperty("page_Mid") && (obj["page_Mid"] is MovieClip))
			{
				_allMiddleMCs[_curXMLIndex]=(obj.hasOwnProperty("page_Mid") && (obj["page_Mid"] is MovieClip))?obj["page_Mid"] as MovieClip:null;
				if(_allMiddleMCs[_curXMLIndex].hasOwnProperty("videoContainer")){
					_allMiddleMCs[_curXMLIndex]["videoContainer"].addChildAt(new loadYouTubeVideo(_allMiddleMCs[_curXMLIndex]["videoContainer"].videoID,_allMiddleMCs[_curXMLIndex]["videoContainer"].videoWidth,_allMiddleMCs[_curXMLIndex]["videoContainer"].videoHeight),0);
				}
				
				
			}
			//evtObj.target.loader.contentLoaderInfo.removeEventListener(ProgressEvent.PROGRESS, loadProgress);
			evtObj.target.loader.contentLoaderInfo.removeEventListener(Event.COMPLETE, LoadEnd);
			obj = null;
			// 本个swf加载完之后,加载下一个swf,
			if (_curXMLIndex<(this._xmlTotalPage-1))
			{
				pb.pm.x+=(pb.pm.width/this._xmlTotalPage);
				//TweenLite.to(pb.pm,0.5,{x:pb.pm.x+(pb.pm.width/this._xmlTotalPage)});
				//all swfs in the xml has been loaded
				_curXMLIndex++;
				SetLoadMC();//Load next xmlswf.
				
			}else{
				pb.pm.x+=(pb.pm.width/this._xmlTotalPage);
				TweenLite.to(pb,1,{alpha:0,onComplete:progressbarCompleteHandler});
				if (onLoadEnd != null){
					onLoadEnd();
					SetDisplayPageMC(book_page);
				}
			}
		}		
		private function progressbarCompleteHandler(){
			mystage.removeChild(pb);
			book_root.parent.visible=true;
			SetDisplayPageMC(book_page);
		}
		/**
		 * 给书添加显示页面,页码从1开始到book_totalpage结束.一次可以添加左右两个页面
		 * "pload_" + pageNum和"pload_" + pageNum+1页
		 * */
		private function SetDisplayPageMC(pageNum:Number):void
		{
			!printflag || trace("SetDisplayPageMC");
			
			pageNum = (pageNum == 0)?pageNum+1:pageNum;
			var p_mc1:MovieClip;
			var p_mc2:MovieClip;
			p_mc1 = all_pagesArray["pload_" + (pageNum)];
			
			//不是第一页,也不是最后一页
			if(pageNum != 1 && pageNum != book_totalpage){
				p_mc2 = all_pagesArray["pload_"+(pageNum+1)];
				p_mc2.x = book_width;
				pageMC.addChild(p_mc1);
				pageMC.addChild(p_mc2);
				//p_mc1.addEventListener(MouseEvent.MOUSE_DOWN, downHandler);
				//p_mc2.addEventListener(MouseEvent.MOUSE_UP, upHandler);
			}
			else{
				//第一页或者最后一页
				p_mc1.x = (pageNum == 1)? book_width:0;
				pageMC.addChild(p_mc1);
				//p_mc1.addEventListener(MouseEvent.MOUSE_DOWN, downHandler);
			}
			pageNum = (pageNum == 1)?0:pageNum;
			if (_allMiddleMCs[pageNum / 2] != null)
			{
				ClearMiddleAnimation();
				MovieClip(_allMiddleMCs[pageNum / 2]).scaleX = globle_scale;
				MovieClip(_allMiddleMCs[pageNum / 2]).scaleY = globle_scale;
				MovieClip(_allMiddleMCs[pageNum / 2]).x = 0;
				MovieClip(_allMiddleMCs[pageNum / 2]).y = 0;
				//trace("insert"+pageNum/2);
				_middleAnimationMC.addChild(_allMiddleMCs[pageNum/2]);
				_middleAnimationMC.showUp(0);

			}
		}		
		//Empty the Mid Animation Container
		private function ClearMiddleAnimation():void
		{

			while (_middleAnimationMC.numChildren!=0)
			{
				_middleAnimationMC.removeChildAt(0);
			}
		}
		
		private function InitpreFlipBtn():void{
			var general_btn_width:Number = 100;
			
			preFlipBtnTL=new preFlipBtn_L();
			preFlipBtnTL.name = "preFlipBtnTL";
			preFlipBtnTL.x = 1;
			preFlipBtnTL.y = 1;//important do not make it 0.
			
			preFlipBtnTR=new preFlipBtn_R();
			preFlipBtnTR.name = "preFlipBtnTR";
			preFlipBtnTR.x = book_width + book_width - general_btn_width-1;
			preFlipBtnTR.y = 1;//important do not make it 0.
			/*
			preFlipBtnBL=new preFlipBtn();
			preFlipBtnBL.name = "preFlipBtnBL";
			preFlipBtnBL.x = 1;
			preFlipBtnBL.y = book_height - general_btn_width-1;
			
			preFlipBtnBR=new preFlipBtn();
			preFlipBtnBR.name = "preFlipBtnBR";
			preFlipBtnBR.x = book_width + book_width - general_btn_width-1;
			preFlipBtnBR.y = book_height - general_btn_width-1;
			book_root.parent.addChild(preFlipBtnBL);
			book_root.parent.addChild(preFlipBtnBR);
			preFlipBtnBL.addEventListener(MouseEvent.MOUSE_OVER,MouseOnOver);
			preFlipBtnBL.addEventListener(MouseEvent.MOUSE_OUT,MouseOnOut);
			preFlipBtnBR.addEventListener(MouseEvent.MOUSE_OVER,MouseOnOver);
			preFlipBtnBR.addEventListener(MouseEvent.MOUSE_OUT,MouseOnOut);
			preFlipBtnBL.addEventListener(MouseEvent.MOUSE_DOWN, MouseOnDown);
			preFlipBtnBL.addEventListener(MouseEvent.MOUSE_UP, MouseOnUp);
			preFlipBtnBR.addEventListener(MouseEvent.MOUSE_DOWN, MouseOnDown);
			preFlipBtnBR.addEventListener(MouseEvent.MOUSE_UP, MouseOnUp);
			*/
			book_root.parent.addChild(preFlipBtnTL);
			book_root.parent.addChild(preFlipBtnTR);
			
			checkPreFlipBtn();
			
			
			
			preFlipBtnTL.addEventListener(MouseEvent.MOUSE_OVER,MouseOnOver);
			preFlipBtnTL.addEventListener(MouseEvent.MOUSE_OUT,MouseOnOut);
			preFlipBtnTR.addEventListener(MouseEvent.MOUSE_OVER,MouseOnOver);
			preFlipBtnTR.addEventListener(MouseEvent.MOUSE_OUT,MouseOnOut);
			
			
			preFlipBtnTL.addEventListener(MouseEvent.MOUSE_DOWN, MouseOnDown);
			preFlipBtnTL.addEventListener(MouseEvent.MOUSE_UP, MouseOnUp);
			preFlipBtnTR.addEventListener(MouseEvent.MOUSE_DOWN, MouseOnDown);
			preFlipBtnTR.addEventListener(MouseEvent.MOUSE_UP, MouseOnUp);
			
			
		}
		private function checkPreFlipBtn():void
		{
			
			preFlipBtnTL.visible=!(book_page<1);
			preFlipBtnTR.visible=!(book_page>=book_totalpage);
			/*
			preFlipBtnBL.visible=!(book_page<1);
			preFlipBtnBR.visible=!(book_page>=book_totalpage);
			*/
		}
		//No.1 **DrawPage Parts------------------------------------------------------------------------
		private function DrawPage(num:Number,_movePoint:Point,bmp1:BitmapData,bmp2:BitmapData):void
		{
			! printflag || trace("DrawPage");
			//var _movePoint:Point=new Point(mouseX,mouseY);
			var _actionPoint:Point;

			var book_array:Array;
			var book_Matrix1:Matrix=new Matrix();
			var book_Matrix2:Matrix=new Matrix();
			var Matrix_angle:Number;


			if (num == 1)
			{

				_movePoint = CheckLimit(_movePoint,limit_point1,book_width);
				_movePoint = CheckLimit(_movePoint,limit_point2,book_CrossGap);

				book_array = GetBook_array(_movePoint,p1,p2);
				_actionPoint = book_array[1];
				GetLayer_array(_movePoint,_actionPoint,p1,p2,limit_point1,limit_point2);

				DrawShadowShap(shadow0,Mask0,book_width*1.5,book_height*4,p1,_movePoint,new Array(p1,p3,p4,p2),0.5);
				DrawShadowShap(shadow1,Mask1,book_width*1.5,book_height*4,p1,_movePoint,bookArray_layer1,0.45);


				Matrix_angle = angle(_movePoint,_actionPoint) + 90;
				book_Matrix1.rotate((Matrix_angle/180)*Math.PI);
				book_Matrix1.tx = book_array[3].x;
				book_Matrix1.ty = book_array[3].y;

				book_Matrix2.tx = p1.x;
				book_Matrix2.ty = p1.y;

			}
			else if (num==2)
			{

				_movePoint = CheckLimit(_movePoint,limit_point2,book_width);
				_movePoint = CheckLimit(_movePoint,limit_point1,book_CrossGap);

				book_array = GetBook_array(_movePoint,p2,p1);
				_actionPoint = book_array[1];
				GetLayer_array(_movePoint,_actionPoint,p2,p1,limit_point2,limit_point1);

				DrawShadowShap(shadow0,Mask0,book_width*1.5,book_height*4,p2,_movePoint,new Array(p1,p3,p4,p2),0.5);
				DrawShadowShap(shadow1,Mask1,book_width*1.5,book_height*4,p2,_movePoint,bookArray_layer1,0.45);

				Matrix_angle = angle(_movePoint,_actionPoint) - 90;
				book_Matrix1.rotate((Matrix_angle/180)*Math.PI);
				book_Matrix1.tx = book_array[2].x;
				book_Matrix1.ty = book_array[2].y;

				book_Matrix2.tx = p1.x;
				book_Matrix2.ty = p1.y;

			}
			else if (num==3)
			{

				_movePoint = CheckLimit(_movePoint,limit_point1,book_width);
				_movePoint = CheckLimit(_movePoint,limit_point2,book_CrossGap);

				book_array = GetBook_array(_movePoint,p3,p4);
				_actionPoint = book_array[1];
				GetLayer_array(_movePoint,_actionPoint,p3,p4,limit_point1,limit_point2);

				DrawShadowShap(shadow0,Mask0,book_width*1.5,book_height*4,p3,_movePoint,new Array(p1,p3,p4,p2),0.5);
				DrawShadowShap(shadow1,Mask1,book_width*1.5,book_height*4,p3,_movePoint,bookArray_layer1,0.4);

				Matrix_angle = angle(_movePoint,_actionPoint) + 90;
				book_Matrix1.rotate((Matrix_angle/180)*Math.PI);
				book_Matrix1.tx = _movePoint.x;
				book_Matrix1.ty = _movePoint.y;

				book_Matrix2.tx = limit_point1.x;
				book_Matrix2.ty = limit_point1.y;

			}
			else
			{

				_movePoint = CheckLimit(_movePoint,limit_point2,book_width);
				_movePoint = CheckLimit(_movePoint,limit_point1,book_CrossGap);

				book_array = GetBook_array(_movePoint,p4,p3);
				_actionPoint = book_array[1];
				GetLayer_array(_movePoint,_actionPoint,p4,p3,limit_point2,limit_point1);

				DrawShadowShap(shadow0,Mask0,book_width*1.5,book_height*4,p4,_movePoint,new Array(p1,p3,p4,p2),0.5);
				DrawShadowShap(shadow1,Mask1,book_width*1.5,book_height*4,p4,_movePoint,bookArray_layer1,0.4);

				Matrix_angle = angle(_movePoint,_actionPoint) - 90;
				book_Matrix1.rotate((Matrix_angle/180)*Math.PI);
				book_Matrix1.tx = _actionPoint.x;
				book_Matrix1.ty = _actionPoint.y;

				book_Matrix2.tx = limit_point1.x;
				book_Matrix2.ty = limit_point1.y;

			}
			//trace(bookArray_layer2)
			//
			DrawShape(render1,bookArray_layer1,bmp1,book_Matrix1);
			DrawShape(render0,bookArray_layer2,bmp2,book_Matrix2);
		}

		private function CheckLimit($point:Point,$limitPoint:Point,$limitGap:Number):Point
		{

			var $Gap:Number = Math.abs(pos($limitPoint,$point));
			var $Angle:Number = angle($limitPoint,$point);
			if ($Gap > $limitGap)
			{
				var $tmp1:Number=$limitGap*Math.sin(($Angle/180)*Math.PI);
				var $tmp2:Number=$limitGap*Math.cos(($Angle/180)*Math.PI);
				$point = new Point($limitPoint.x - $tmp2,$limitPoint.y - $tmp1);
			}
			return $point;

		}

		//No. 2
		private function GetBook_array($point:Point,$actionPoint1:Point,$actionPoint2:Point):Array
		{
			! printflag || trace("GetBookArray");

			var array_return:Array=new Array();
			var $Gap1:Number = Math.abs(pos($actionPoint1,$point) * 0.5);
			var $Angle1:Number = angle($actionPoint1,$point);
			var $tmp1_2:Number=$Gap1/Math.cos(($Angle1/180)*Math.PI);
			var $tmp_point1:Point = new Point($actionPoint1.x - $tmp1_2,$actionPoint1.y);

			var $Angle2:Number = angle($point,$tmp_point1) - angle($point,$actionPoint2);
			var $Gap2:Number = pos($point,$actionPoint2);
			var $tmp2_1:Number=$Gap2*Math.sin(($Angle2/180)*Math.PI);
			var $tmp2_2:Number=$Gap2*Math.cos(($Angle2/180)*Math.PI);
			var $tmp_point2:Point = new Point($actionPoint1.x + $tmp2_2,$actionPoint1.y + $tmp2_1);

			var $Angle3:Number = angle($tmp_point1,$point);
			var $tmp3_1:Number=book_width*Math.sin(($Angle3/180)*Math.PI);
			var $tmp3_2:Number=book_width*Math.cos(($Angle3/180)*Math.PI);

			var $tmp_point3:Point = new Point($tmp_point2.x + $tmp3_2,$tmp_point2.y + $tmp3_1);
			var $tmp_point4:Point = new Point($point.x + $tmp3_2,$point.y + $tmp3_1);

			array_return.push($point);
			array_return.push($tmp_point2);
			array_return.push($tmp_point3);
			array_return.push($tmp_point4);

			return array_return;

		}


		// No.3
		private function GetLayer_array($point1:Point,$point2:Point,$actionPoint1:Point,$actionPoint2:Point,$limitPoint1:Point,$limitPoint2:Point):void
		{
			! printflag || trace("GetLayerArrary");

			var array_layer1:Array=new Array();
			var array_layer2:Array=new Array();
			var $Gap1:Number = Math.abs(pos($actionPoint1,$point1) * 0.5);
			var $Angle1:Number = angle($actionPoint1,$point1);

			var $tmp1_1:Number=$Gap1/Math.sin(($Angle1/180)*Math.PI);
			var $tmp1_2:Number=$Gap1/Math.cos(($Angle1/180)*Math.PI);

			var $tmp_point1:Point = new Point($actionPoint1.x - $tmp1_2,$actionPoint1.y);
			var $tmp_point2:Point = new Point($actionPoint1.x,$actionPoint1.y - $tmp1_1);

			var $tmp_point3 = $point2;

			var $Gap2:Number = Math.abs(pos($point1,$actionPoint2));
			//---------------------------------------------
			if ($Gap2 > book_height)
			{
				array_layer1.push($tmp_point3);
				//;
				var $pos:Number = Math.abs(pos($tmp_point3,$actionPoint2) * 0.5);
				var $tmp3:Number=$pos/Math.cos(($Angle1/180)*Math.PI);
				$tmp_point2 = new Point($actionPoint2.x - $tmp3,$actionPoint2.y);

			}
			else
			{
				array_layer2.push($actionPoint2);
			}
			array_layer1.push($tmp_point2);
			array_layer1.push($tmp_point1);
			array_layer1.push($point1);
			bookArray_layer1 = array_layer1;

			array_layer2.push($limitPoint2);
			array_layer2.push($limitPoint1);
			array_layer2.push($tmp_point1);
			array_layer2.push($tmp_point2);
			bookArray_layer2 = array_layer2;

		}

		private function DrawShape(shape:Shape,point_array:Array,myBmp:BitmapData,matr:Matrix):void
		{
			var num = point_array.length;
			shape.graphics.clear();
			shape.graphics.beginBitmapFill(myBmp,matr,false,true);

			shape.graphics.moveTo(point_array[0].x,point_array[0].y);
			for (var i=1; i<num; i++)
			{
				shape.graphics.lineTo(point_array[i].x,point_array[i].y);
			}
			shape.graphics.endFill();
		}

		private function DrawShadowShap(shape:Shape,maskShape:Shape,g_width:Number,g_height:Number,$point1:Point,$point2:Point,$maskArray:Array,$arg:Number):void
		{

			var fillType:String = GradientType.LINEAR;
			var colors:Array = [0x0,0x0];
			var alphas1:Array = [0,0.5];
			var alphas2:Array = [0.5,0];
			var ratios:Array = [0,255];
			var matr:Matrix = new Matrix();
			var spreadMethod:String = SpreadMethod.PAD;
			var myscale:Number;
			var myalpha:Number;

			shape.graphics.clear();
			matr.createGradientBox(g_width, g_height, (0/180)*Math.PI,-g_width*0.5, -g_height*0.5);

			shape.graphics.beginGradientFill(fillType, colors, alphas1, ratios, matr, spreadMethod);
			shape.graphics.drawRect(-g_width*0.5,-g_height*0.5,g_width*0.5,g_height);
			shape.graphics.beginGradientFill(fillType, colors, alphas2, ratios, matr, spreadMethod);
			shape.graphics.drawRect(0,-g_height*0.5,g_width*0.5,g_height);

			shape.x=$point2.x+($point1.x-$point2.x)*$arg;
			shape.y=$point2.y+($point1.y-$point2.y)*$arg;
			shape.rotation = angle($point1,$point2);
			myscale = Math.abs($point1.x - $point2.x) * 0.5 / book_width;
			myalpha = 1 - myscale * myscale;

			shape.scaleX = myscale + 0.1;
			shape.alpha = myalpha + 0.1;

			var tmp_Bmp:BitmapData = new BitmapData(book_width * 2,book_height,true,0x0);
			DrawShape(maskShape,$maskArray,tmp_Bmp,new Matrix());
			shape.mask = maskShape;

		}





		//;
		private function Add_ShowMiddleAnimation(index):void
		{

		}
		/*
		* when swf is loading show processing ring and loading processing.
		*/
		private function loadProgress(evtObj:ProgressEvent):void
		{
			//trace("loadProgress");

			var obj = evtObj.currentTarget;
			//var n:Number=(LoadFindLoader(obj));      //Removable
			var percentLoaded:Number = evtObj.bytesLoaded / evtObj.bytesTotal;

			percentLoaded = Math.round(percentLoaded * 100);

			if (onLoading != null)
			{
				onLoading(all_pagesArray["pload_" + _curPageIndex],percentLoaded);
			}
		}

		//**MouseEvent Parts------------------------------------------------------------------------
		private function MouseOnDown(evt:Event):void
		{
			//printflag=true;
			Dragflag = 0;
			if (getQualifiedClassName(evt.target) == "preFlipBtn" && book_TimerFlag == "stop")
			{
				book_TimerArg1 = MouseFindArea(new Point(book_root.mouseX,book_root.mouseY));
				book_TimerFlag = "autoplay";
				PageUp();
			}
		}
		private function MouseOnUp(evt:Event):void
		{
			if (book_TimerFlag == "startplay")
			{
				//处于mousedown状态时
				book_TimerArg1 = MouseFindArea(new Point(book_root.mouseX,book_root.mouseY));
				book_TimerFlag = "autoplay";
			}
			Dragflag = 0;
			//evt.stopImmediatePropagation();
		}
		private function MouseOnOver(evt:Event):void
		{
			book_TimerArg0 =  -  MouseFindArea(new Point(book_root.mouseX,book_root.mouseY));
			book_TimerFlag = "startplay";
			PageUp();
		}
		private function MouseOnOut(evt:Event):void
		{
			if (book_TimerFlag == "startplay" && Dragflag == 0)
			{
				//处于mousedown状态时
				book_TimerFlag = "fliprelease";
			}
			if (Dragflag == 0)
			{
				//TweenLite.to(_middleAnimationMC,2,{alpha:1});
				_middleAnimationMC.visible=true;
			}
		}
		private function MouseFindArea(point:Point):Number
		{

			var tmpn:Number = 0;
			var minx:Number = 0;
			var maxx:Number = book_width + book_width;
			var miny:Number = 0;
			var maxy:Number = book_height;
			var areaNum:Number = 120;


			if (point.x > minx && point.x <= maxx * 0.5)
			{
				tmpn=(point.y>miny&&point.y<=(maxy*0.5))?1:(point.y>(maxy*0.5)&&point.y<maxy)?2:0;
				if (point.x<=(minx+areaNum))
				{
					tmpn=(point.y>miny&&point.y<=(miny+areaNum))?-1:(point.y>(maxy-areaNum)&&point.y<maxy)?-2:tmpn;
				}
				return tmpn;
			}
			else if (point.x>(maxx*0.5)&&point.x<maxx)
			{
				tmpn=(point.y>miny&&point.y<=(maxy*0.5))?3:(point.y>(maxy*0.5)&&point.y<maxy)?4:0;
				if (point.x>=(maxx-areaNum))
				{
					tmpn=(point.y>miny&&point.y<=(miny+areaNum))?-3:(point.y>(maxy-areaNum)&&point.y<maxy)?-4:tmpn;
				}
				return tmpn;
			}

			return 0;
		}

		//**Page Parts------------------------------------------------------------------------
		public function PageGoto(topage:Number):void
		{
			//output.setHtmlText("call Page:"+topage);
			if(topage!=book_page){
				//! printflag || trace(0);
				ClearMiddleAnimation();
				var n:Number;
				topage = topage % 2 == 1 ? topage - 1:topage;
				n = topage - book_page;
				if (book_TimerFlag == "stop" && topage >= 0 && topage <= book_totalpage && n != 0)
				{
					book_TimerArg0 = n < 0 ? 1:3;
					book_TimerArg1 = -1;
					book_topage = topage > book_totalpage ? book_totalpage:topage;
					book_TimerFlag = "autoplay";
					PageUp();
				}
			}
		}
		public function PageDraw(pageNum:Number):BitmapData
		{
			var myBmp:BitmapData = new BitmapData(book_width,book_height,true,0x000000);
			if (pageNum > 0 && pageNum <= book_totalpage)
			{
				/**
				 * Matrix(a:Number = 1, b:Number = 0, c:Number = 0, d:Number = 1, tx:Number = 0, ty:Number = 0) 
				 * */
					var matrix:Matrix = new Matrix();
					matrix.scale(globle_scale, globle_scale);
					myBmp.draw(all_pagesArray["pload_"+pageNum],matrix);
			}
			return myBmp;
		}
		private function PageUp():void
		{
			var page1:Number;
			var page2:Number;
			var page3:Number;
			var page4:Number;
			var point_mypos:Point=(book_TimerArg0!=0)?book_myposArray[book_TimerArg0-1]:book_myposArray[0];
			//var point_mypos:Point=book_myposArray[book_TimerArg0-1];
			var b0:Bitmap;
			var b1:Bitmap;
			if (book_TimerArg0 == 1 || book_TimerArg0 == 2)
			{//鼠标在左上角或左下角.

				book_topage = book_topage == book_page ? book_page - 2:book_topage;
				page1 = book_page;
				page2 = book_topage + 1;
				page3 = book_topage;
				page4 = book_page + 1;

			}
			else if (book_TimerArg0==3||book_TimerArg0==4)
			{ //鼠标在右上角或右下角.

				book_topage = book_topage == book_page ? book_page + 2:book_topage;
				page1 = book_page + 1;
				page2 = book_topage;
				page3 = book_page;
				page4 = book_topage + 1;
			}
			book_px = point_mypos.x;
			book_py = point_mypos.y;

			Bmp0 = PageDraw(page1);
			Bmp1 = PageDraw(page2);
			bgBmp0 = PageDraw(page3);
			bgBmp1 = PageDraw(page4);
			
			b0 = new Bitmap(bgBmp0);
			b1 = new Bitmap(bgBmp1);
			
			b1.x = book_width;
			bgMC.addChild(b0);
			bgMC.addChild(b1);
			bgMC.visible = false;
			book_timer.start();

		}

		//**Timer Parts------------------------------------------------------------------------
		private function bookTimerHandler(event:TimerEvent):void
		{

			var point_topos:Point = book_toposArray[book_TimerArg0 - 1];// the drag end point 
			var point_mypos:Point = book_myposArray[book_TimerArg0 - 1];// the drag start point 

			var PageObj:Object;
			var array_point1:Array;
			var array_point2:Array;
			var numpoint1:Number;
			var numpoint2:Number;

			var tmpMC0:MovieClip;
			var tmpageMC0:MovieClip;

			var tox:Number;
			var toy:Number;
			var toflag:Number;
			var tmpx:Number;
			var tmpy:Number;

			var arg:Number;
			var r:Number;
			var a:Number;

			bgMC.visible = true;

			while (pageMC.numChildren>0)
			{
				pageMC.removeChildAt(0);
				//remove all the element in pageMC;

				if (book_page > 0 && book_page <= book_totalpage)
				{//current bookpage still valid.
					tmpMC0 = all_pagesArray["pload_" + book_page];
					if (tmpMC0.loadedflag == true && tmpMC0.loadedtype == "application/x-shockwave-flash")
					{
						tmpageMC0 = tmpMC0["loader"].content;
						tmpageMC0.gotoAndStop(1);
					}
				}
				if ((book_page+1)>0&&(book_page+1)<=book_totalpage)
				{
					tmpMC0=all_pagesArray["pload_"+(book_page+1)];
					if (tmpMC0.loadedflag == true && tmpMC0.loadedtype == "application/x-shockwave-flash")
					{
						tmpageMC0 = tmpMC0["loader"].content;
						tmpageMC0.gotoAndStop(1);
					}
				}
			}
			if (book_TimerFlag == "startplay")
			{
				u = 0.4;
				render0.graphics.clear();
				render1.graphics.clear();
				book_px=((render0.mouseX-book_px)*u+book_px)>>0;
				book_py=((render0.mouseY-book_py)*u+book_py)>>0;
				book_py = Boolean(book_py == 0) ? 1:book_py;
				DrawPage(book_TimerArg0,new Point(book_px,book_py),Bmp1,Bmp0);
				//book_timer.stop();
			}
			else if (book_TimerFlag=="autoplay")
			{
				flipOnProcess();
				render0.graphics.clear();
				render1.graphics.clear();
				
					tox = point_topos.x;
					toy = point_topos.y;
					toflag = 1;
				
				tmpx=(tox-book_px)>>0;
				tmpy=(toy-book_py)>>0;

				if (book_TimerArg1 < 0)
				{
					//处于点翻区域时
					u = 0.3;//降低加速度
					book_py = Arc(book_width,tmpx,point_topos.y);
				}
				else
				{
					u = 0.4;//原始加速度
					book_py = tmpy * u + book_py;
				}
				book_px = tmpx * u + book_px;

				DrawPage(book_TimerArg0,new Point(book_px,book_py),Bmp1,Bmp0);

				if (tmpx == 0 && tmpy == 0)
				{

					render0.graphics.clear();
					render1.graphics.clear();
					shadow0.graphics.clear();
					shadow1.graphics.clear();

					Bmp0.dispose();
					Bmp1.dispose();
					bgBmp0.dispose();
					bgBmp1.dispose();

					while (bgMC.numChildren>0)
					{
						bgMC.removeChildAt(0);
					}
					book_topage = toflag == 0 ? book_page:book_topage;
					book_page = book_topage;

					SetDisplayPageMC(book_page);

					book_TimerFlag = "stop";//恢得静止状态
					if (onPageEnd != null)
					{
						onPageEnd();
					}

					bgMC.visible = false;
					book_timer.reset();
					trace("called------------------");
					checkPreFlipBtn();

				}
			}
			else if (book_TimerFlag=="fliprelease")
			{
				
				u = 0.4;
				render0.graphics.clear();
				render1.graphics.clear();
				DrawPage(book_TimerArg0,point_mypos,Bmp1,Bmp0);
				
			}
		}

		//**Tools Parts------------------------------------------------------------------------
		private function Arc(arg_R,arg_N1,arg_N2):Number
		{
			//------圆弧算法-----------------------
			var arg = arg_R * 2;
			var r = arg_R * arg_R + arg * arg;
			var a = Math.abs(arg_N1) - arg_R;
			var R_arg:Number=arg_N2 - (Math.sqrt(r-a*a)-arg);
			return R_arg;
		}
		private function angle(target1,target2):Number
		{
			var tmp_x:Number = target1.x - target2.x;
			var tmp_y:Number = target1.y - target2.y;
			var tmp_angle:Number = Math.atan2(tmp_y,tmp_x) * 180 / Math.PI;
			tmp_angle = tmp_angle < 0 ? tmp_angle + 360:tmp_angle;
			return tmp_angle;
		}
		private function pos(target1,target2):Number
		{
			var tmp_x:Number = target1.x - target2.x;
			var tmp_y:Number = target1.y - target2.y;
			var tmp_s:Number = Math.sqrt(tmp_x * tmp_x + tmp_y * tmp_y);
			return target1.x > target2.x?tmp_s:- tmp_s;
		}

		private function flipOnProcess():void{
			preFlipBtnTL.visible=preFlipBtnTR.visible=false;
			_middleAnimationMC.dispear(100);
			this.ClearMiddleAnimation();
			//preFlipBtnBL.visible=preFlipBtnBR.visible=false;
		}
		
		
	}
}